import logging
import allure
from utils.send_request import send_jdbc_request
from utils.get_keywords import GetKeywords
from utils.base_asserts import MainAsserts
from abc import ABC, abstractmethod
import pytest


class BaseJdbcAssert(ABC):
    @abstractmethod
    def jdbc_assert(self, case):
        pass



class SingleJdbcAssert(BaseJdbcAssert):
    def jdbc_assert(self, case):
        """
        jdbc--java的叫法
        在python连接数据库使用的是pymysql
        """
        if case["sql_check"] and case["sql_expected"]:
            with allure.step("3.JDBC响应断言"):
                result = send_jdbc_request(case["sql_check"])
                # logging.info(f"3.JDBC响应断言内容: 实际结果({result}) == 预期结果({case['sql_expected']})")
                # assert result == case["sql_expected"]
                logging.info(f"3.JDBC响应断言内容: 实际结果[预期结果({case['sql_expected']}) in ({result})]")
                assert case["sql_expected"] in str(result)



class MultipleJdbcAssert(BaseJdbcAssert):
    """多重数据库断言"""
    def jdbc_assert(self, case):
        if case["sql_check"] and case["sql_expected"]:
            with allure.step("3.JDBC多重响应断言"):
                sql_check = eval(case["sql_check"])
                sql_expected = eval(case["sql_expected"])
                for sql, expected in zip(sql_check, sql_expected):
                    result = send_jdbc_request(sql)
                    logging.info(f"3.JDBC多重响应断言内容: 实际结果[预期结果({expected}) in ({result})]")
                    pytest.assume(expected in str(result))


def obj_jdbc_processor(case):
    """
    对象处理，根据传入的case的不同，来返回不同的对象
    :return:
    """
    try:
        eval(case["sql_check"])
    except:
        return SingleJdbcAssert()
    else:
        return MultipleJdbcAssert()


def jdbc_assert(case):
    """
    jdbc真正的断言逻辑
    :param case: 测试用例
    :return:
    """
    obj_jdbc_processor(case).jdbc_assert(case)